package io.github.droidkaigi.confsched.primitive

import kotlinx.kover.gradle.plugin.dsl.KoverReportExtension
import org.gradle.api.Plugin
import org.gradle.api.Project
import org.gradle.kotlin.dsl.configure

class KoverEntryPointPlugin : Plugin<Project> {
    override fun apply(target: Project) {
        val koverPlugin = "org.jetbrains.kotlinx.kover"
        with(target) {
            pluginManager.apply(koverPlugin)

            rootProject.subprojects {
                if (this@subprojects.name == target.name) return@subprojects
                // https://github.com/DroidKaigi/conference-app-2024/issues/485#issuecomment-2304251937
                this@subprojects.beforeEvaluate { // wrap with beforeEvaluate
                    this@subprojects.pluginManager.apply(koverPlugin)
                }
                target.dependencies.add("kover", this@subprojects)
            }

            configure<KoverReportExtension> {
                filters {
                    excludes {
                        packages(
                            "dagger.hilt.*",
                            "hilt_aggregated_deps",
                        )
                        classes(
                            // classes generated by Hilt
                            "Hilt_*",
                            "*_Factory",
                            "*_HiltModules*",
                            "*Module_Provide*Factory",
                            // compose previews
                            "*Preview*Kt",
                        )
                    }
                }
            }
        }
    }
}
